package com.speedata.testsimple;

import static android.os.VibrationEffect.DEFAULT_AMPLITUDE;
import static android.os.VibrationEffect.MAX_AMPLITUDE;

import android.annotation.SuppressLint;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Build;
import android.os.Bundle;
import android.os.VibrationEffect;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import com.speedata.MyApp;
import com.speedata.uhftest.R;
import com.speedata.utils.ToastUtils;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

/**
 * @author xuyan  UDT50  震动
 */
public class VbtestActivity extends AppCompatActivity implements View.OnClickListener {

    private Button bt1;
    private Button bt2;

    private Button bt3;
    private Button bt4;

    private Button bt5;
    private Button bt6;

    private Button bt7;
    private Button bt8;

    private Button bt9;
    private Button bt10;

    //gpio2_a0 gpio2_a1 gpio2_a2 gpio2_a5
    private String path1 = "sys/devices/platform/function_gpio/gpio2_a0";
    private String path2 = "sys/devices/platform/function_gpio/gpio2_a1";
    private String path3 = "sys/devices/platform/function_gpio/gpio2_a2";
    private String path4 = "sys/devices/platform/function_gpio/gpio2_a5";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_vbtest);
        initView();
    }

    private void initView() {
        bt1 = findViewById(R.id.bt1);
        bt2 = findViewById(R.id.bt2);
        bt3 = findViewById(R.id.bt3);
        bt4 = findViewById(R.id.bt4);
        bt5 = findViewById(R.id.bt5);
        bt6 = findViewById(R.id.bt6);
        bt7 = findViewById(R.id.bt7);
        bt8 = findViewById(R.id.bt8);
        bt9 = findViewById(R.id.bt9);
        bt10 = findViewById(R.id.bt10);

        bt1.setOnClickListener(this);
        bt2.setOnClickListener(this);
        bt3.setOnClickListener(this);
        bt4.setOnClickListener(this);
        bt5.setOnClickListener(this);
        bt6.setOnClickListener(this);
        bt7.setOnClickListener(this);
        bt8.setOnClickListener(this);
        bt9.setOnClickListener(this);
        bt10.setOnClickListener(this);

    }


    @RequiresApi(api = Build.VERSION_CODES.O)
    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt1:
                try {
                    MyApp.getInstance().getVibrator().vibrate(VibrationEffect.createOneShot(100, DEFAULT_AMPLITUDE));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                ToastUtils.showLongToastSafe("100毫秒默认强度");
                break;
            case R.id.bt2:
                try {
                    MyApp.getInstance().getVibrator().vibrate(VibrationEffect.createOneShot(100, MAX_AMPLITUDE));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                ToastUtils.showLongToastSafe("100毫秒最大强度");
                break;
            case R.id.bt3:
                try {
                    MyApp.getInstance().getVibrator().vibrate(VibrationEffect.createOneShot(200, DEFAULT_AMPLITUDE));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                ToastUtils.showLongToastSafe("200毫秒默认强度");
                break;
            case R.id.bt4:
                try {
                    MyApp.getInstance().getVibrator().vibrate(VibrationEffect.createOneShot(200, MAX_AMPLITUDE));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                ToastUtils.showLongToastSafe("200毫秒最大强度");
                break;
            case R.id.bt5:
                try {
                    MyApp.getInstance().getVibrator().vibrate(VibrationEffect.createOneShot(300, DEFAULT_AMPLITUDE));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                ToastUtils.showLongToastSafe("300毫秒默认强度");
                break;
            case R.id.bt6:
                try {
                    MyApp.getInstance().getVibrator().vibrate(VibrationEffect.createOneShot(300, MAX_AMPLITUDE));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                ToastUtils.showLongToastSafe("300毫秒最大强度");
                break;
            case R.id.bt7:
                try {
                    MyApp.getInstance().getVibrator().vibrate(VibrationEffect.createOneShot(400, DEFAULT_AMPLITUDE));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                ToastUtils.showLongToastSafe("400毫秒默认强度");
                break;
            case R.id.bt8:
                try {
                    MyApp.getInstance().getVibrator().vibrate(VibrationEffect.createOneShot(400, MAX_AMPLITUDE));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                ToastUtils.showLongToastSafe("400毫秒最大强度");
                break;

            case R.id.bt9:
                try {
                    MyApp.getInstance().getVibrator().vibrate(VibrationEffect.createOneShot(500, DEFAULT_AMPLITUDE));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                ToastUtils.showLongToastSafe("500毫秒默认强度");
                break;

            case R.id.bt10:
                try {
                    MyApp.getInstance().getVibrator().vibrate(VibrationEffect.createOneShot(500, MAX_AMPLITUDE));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                ToastUtils.showLongToastSafe("500毫秒最大强度");
                break;
            default:
                break;
        }
    }

    private void doStartApplicationWithPackageName(String packagename) {

        // 通过包名获取此APP详细信息，包括Activities、services、versioncode、name等等
        PackageInfo packageinfo = null;
        try {
            packageinfo = getPackageManager().getPackageInfo(packagename, 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        if (packageinfo == null) {
            return;
        }

        // 创建一个类别为CATEGORY_LAUNCHER的该包名的Intent
        Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
        resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        resolveIntent.setPackage(packageinfo.packageName);

        // 通过getPackageManager()的queryIntentActivities方法遍历
        List<ResolveInfo> resolveinfoList = getPackageManager()
                .queryIntentActivities(resolveIntent, 0);

        ResolveInfo resolveinfo = resolveinfoList.iterator().next();
        if (resolveinfo != null) {
            // packagename = 参数packname
            String packageName = resolveinfo.activityInfo.packageName;
            // 这个就是我们要找的该APP的LAUNCHER的Activity[组织形式：packagename.mainActivityname]
            String className = resolveinfo.activityInfo.name;
            // LAUNCHER Intent
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_LAUNCHER);

            // 设置ComponentName参数1:packagename参数2:MainActivity路径
            ComponentName cn = new ComponentName(packageName, className);

            intent.setComponent(cn);
            startActivity(intent);
        }
    }


    private static BufferedWriter TorchFileWrite;

    /**
     * imbox,gpio
     *
     * @param str "1"  "0"
     */
    public static void open(String path, String str) {
        File TorchFileName = new File(path);
        try {
            TorchFileWrite = new BufferedWriter(new FileWriter(TorchFileName, false));
            TorchFileWrite.write(str);
            TorchFileWrite.flush();
            TorchFileWrite.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
